package trade

import (
	"errors"
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo/options"
	"higame/config"
	mongodb "higame/modules/db"
)

type GoodsType string
type SafeType string
type Status int64

const (
	Account GoodsType = "0"
	Gun     GoodsType = "1"
	Gold    GoodsType = "2"
	Admin   SafeType  = "0"
	InSell  Status    = 0 // 出售
	Payment Status    = 1 // 付款
	Ship    Status    = 2 // 发货
	Receipt Status    = 3 // 收货
	Success Status    = 4 // 完成
)

var db = mongodb.GetInstance()
var colName = config.Db.Collection.Trade

type Schema struct {
	ID          primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
	UUID        string             `json:"uuid" bson:"uuid" form:"uuid"`
	AddTime     int64              `json:"add_time" bson:"add_time" form:"add_time"`
	EndTime     int64              `json:"end_time" bson:"end_time" form:"end_time" binding:"required"`
	Title       string             `json:"title" bson:"title" form:"title" binding:"required,min=5,max=50"`
	Subtitle    string             `json:"subtitle" bson:"subtitle" form:"subtitle"`
	Price       float64            `json:"price" bson:"price" form:"price" binding:"required"`
	Quantity    int64              `json:"quantity" bson:"quantity" form:"quantity" binding:"required"`
	GoodsType   GoodsType          `json:"goods_type" bson:"goods_type" form:"goods_type" binding:"required"`
	Belongs     string             `json:"belongs" bson:"belongs" form:"belongs" binding:"required"`
	Seller      string             `json:"seller" bson:"seller" form:"seller" binding:"required"`
	MainImage   string             `json:"main_image" bson:"main_image" form:"main_image"`
	DetailImage []string           `json:"detail_image" bson:"detail_image" form:"detail_image"`
	SafeType    SafeType           `json:"safe_type" bson:"safe_type" form:"safe_type" binding:"required"`
}

func New() *Schema {
	return &Schema{}
}

func Parse(data bson.M) (*Schema, error) {
	u := New()
	if err := db.ToStruct(data, u); err != nil {
		return nil, err
	}
	return u, nil
}

func DeleteOneByUUID(uuid string) error {
	b := db.DeleteOne(colName, bson.M{"uuid": uuid})
	if !b {
		return errors.New("删除失败")
	}
	return nil
}

func AddOne(schema *Schema) error {
	adminI := db.InsertOne(colName, schema)
	if !adminI {
		return fmt.Errorf("添加失败。\n")
	}
	return nil
}

func FindOneByUUID(uuid string) (*Schema, error) {
	userF := db.FindOne(colName, bson.M{"uuid": uuid})
	if userF == nil {
		return nil, errors.New("数据不存在！")
	}

	return Parse(userF)
}

func FindMany(pageNum int64, filter interface{}) ([]bson.M, error) {
	pageNum = pageNum - 1
	if pageNum < 0 {
		pageNum = 0
	}
	opts := new(options.FindOptions)
	list := db.FindMany(colName, filter,
		opts.SetSkip(pageNum*20),
		opts.SetLimit(20),
		opts.SetSort(bson.M{
			"add_time": -1,
		}))
	return list, nil
}

func UpdateOneByUUID(uuid string, update interface{}) error {
	b := db.UpdateOne(colName, bson.M{
		"uuid": uuid,
	}, bson.M{
		"$set": update,
	})

	if !b {
		return errors.New("没有修改任何数据")
	}
	return nil
}

func UpdateOne(filter interface{}, update interface{}) error {
	b := db.UpdateOne(colName, filter, update)

	if !b {
		return errors.New("没有修改任何数据")
	}
	return nil
}

func Count(filter interface{}) (int64, error) {
	return db.Count(colName, filter), nil
}
